package cn.xuqiudong.sso.shiro;


import org.apache.shiro.session.mgt.SimpleSession;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 描述:
 *   SimpleSession 的中继类
 *   在存储到redis前把 SimpleSession 转为 RelaySimpleSession
 *   在从redis恢复时，再把RelaySimpleSession 转为 SimpleSession
 * @author Vic.xu
 * @since 2025-04-18 16:09
 */
public class RelaySimpleSession implements Serializable {

    private static final long serialVersionUID = 1L;

    private Serializable id;
    private Date startTimestamp;
    private Date stopTimestamp;
    private Date lastAccessTime;
    private long timeout;
    private boolean expired;
    private String host;
    private Map<Object, Object> attributes;

    public RelaySimpleSession() {}

    /**
     * 构造器：从 SimpleSession 拷贝所有重要字段
     */
    public RelaySimpleSession(SimpleSession source) {
        this.id = source.getId();
        this.startTimestamp = source.getStartTimestamp();
        this.stopTimestamp = source.getStopTimestamp();
        this.lastAccessTime = source.getLastAccessTime();
        this.timeout = source.getTimeout();
        this.expired = source.isExpired();
        this.host = source.getHost();
        Map<Object, Object> attrs = source.getAttributes();
        if (attrs != null) {
            this.attributes = new HashMap<>(attrs);
        } else {
            this.attributes = new HashMap<>();
        }
    }

    /**
     * 转换回 SimpleSession 实例，用于 Shiro 使用
     */
    public SimpleSession toSimpleSession() {
        SimpleSession session = new SimpleSession();
        session.setId(this.id);
        session.setStartTimestamp(this.startTimestamp);
        session.setStopTimestamp(this.stopTimestamp);
        session.setLastAccessTime(this.lastAccessTime);
        session.setTimeout(this.timeout);
        session.setExpired(this.expired);
        session.setHost(this.host);
        if (this.attributes != null) {
            for (Map.Entry<Object, Object> entry : this.attributes.entrySet()) {
                session.setAttribute(entry.getKey(), entry.getValue());
            }
        }
        return session;
    }

    // 以下是标准 getter 和 setter，确保可序列化框架支持（如 Jackson）

    public Serializable getId() {
        return id;
    }

    public void setId(Serializable id) {
        this.id = id;
    }

    public Date getStartTimestamp() {
        return startTimestamp;
    }

    public void setStartTimestamp(Date startTimestamp) {
        this.startTimestamp = startTimestamp;
    }

    public Date getStopTimestamp() {
        return stopTimestamp;
    }

    public void setStopTimestamp(Date stopTimestamp) {
        this.stopTimestamp = stopTimestamp;
    }

    public Date getLastAccessTime() {
        return lastAccessTime;
    }

    public void setLastAccessTime(Date lastAccessTime) {
        this.lastAccessTime = lastAccessTime;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public boolean isExpired() {
        return expired;
    }

    public void setExpired(boolean expired) {
        this.expired = expired;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Map<Object, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<Object, Object> attributes) {
        this.attributes = attributes;
    }
}
